﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using QSHC.QSchedule.Silverlight.ViewModels;
using QSHC.QSchedule.Silverlight.Helpers;
using QSHC.QSchedule.Web.Models;

namespace QSHC.QSchedule.Silverlight.Views
{
    public partial class PatientAppointmentPage : Page
    {
        private CheckBox selectedAllsCheckBox;

        public PatientAppointmentPage()
        {
            InitializeComponent();

            SetBlackoutDates();

            PostponeLocationComboBox.SelectionChanged += (s, e) => SetBlackoutDates();
            PostponePhysicianACCB.SelectionChanged += (s, e) => SetBlackoutDates();
            PostponeScheduleDatePicker.DisplayDateChanged += (s, e) => SetBlackoutDates();
            SearchTextBox.KeyUp += new KeyEventHandler(SearchTextBox_KeyUp);
        }

        void SearchTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            var vm = Resources["patientAppointmentViewModel"] as PatientAppointmentViewModel;
            vm.SearchText = SearchTextBox.Text;
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private void SelectedAllCheckBox_Loaded(object sender, RoutedEventArgs e)
        {
            selectedAllsCheckBox = sender as CheckBox;
        }

        private void SelectedAllCheckBox_Click(object sender, RoutedEventArgs e)
        {
            var vm = Resources["patientAppointmentViewModel"] as PatientAppointmentViewModel;
            vm.SelectAllAppointment(selectedAllsCheckBox.IsChecked == null ? false : selectedAllsCheckBox.IsChecked.Value);
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            selectedAllsCheckBox.IsChecked = false;
        }

        private void SetBlackoutDates()
        {
            var location = PostponeLocationComboBox.SelectedValue as Service;
            var physician = PostponePhysicianACCB.SelectedItem as Employee;

            PostponeScheduleDatePicker.BlackoutDates.Clear();
            var blackoutDates = CalendarHelper.GetBlackoutDates(location, physician, PostponeScheduleDatePicker.DisplayDate);

            if (PostponeScheduleDatePicker.SelectedDate != null)
            {
                if (blackoutDates.Where(p => p.Date == PostponeScheduleDatePicker.SelectedDate.Value.Date).Count() > 0)
                    PostponeScheduleDatePicker.SelectedDate = null;
            }

            foreach (var item in blackoutDates)
            {
                PostponeScheduleDatePicker.BlackoutDates.Add(new CalendarDateRange(item));
            }
        }
    }
}
